package com.bana.fvs.app.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bana.fvs.R;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.module.detaile.entity.PopUpInfo;
import com.wei.commonlibrary.utils.DisplayUtil;
import com.wei.commonlibrary.utils.GlobalContext;

import java.util.List;

/**
 * Created by ${wei} on 2017/4/5.
 */

public class MyPopUpwindow extends PopupWindow {

    private final ListView listView;
    private final View contentView;
    private OnItemClickListener mListener;

    public MyPopUpwindow(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.layout_popupwindow_list, null, false);

        listView = (ListView) contentView.findViewById(R.id.pop_listview);
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width = display.getWidth();
        setContentView(contentView);
        setWidth((int) (width * 0.3));
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        this.update();
    }

    public void setData(List<PopUpInfo> list) {
        listView.setAdapter(new CommonAdapter<PopUpInfo>(list, R.layout.item_layout_popupwindow) {
            @Override
            public void convert(ViewHolder viewHolder, PopUpInfo popUpItemBean) {
                viewHolder.setText(R.id.item_popup_tv, popUpItemBean.getText());
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    mListener.onItemClick(position);
                    dismiss();
                }
            }
        });

        setWidth((int) (DisplayUtil.getScreenWH(GlobalContext.context)[0] * 0.3));
        setHeight(220);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mListener != null) {
                    mListener.onDismiss();
                }
            }
        });
    }

    public void show(View parent, int left) {
        if (this.isShowing()) {
            dismiss();
        } else {
            showAsDropDown(parent, left, 0);
        }
    }

    public void show(View view, int left, int right) {
        if (right > 0 && right > left) {
            setWidth(right - left);
        }
        show(view, left);
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onDismiss();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

}